//
//  FinancialDetailsVC.m
//  phmall
//
//  Created by admin on 2017/11/13.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "FinancialDetailsVC.h"
#import "FinancialDetailsCell.h"
#import "MoneyRechargeVC.h"
#import "BuyPointsViewController.h"
#import "DetailViewController.h"
#import "IntegralViewController.h"
#import "UserInfoModel.h"
@interface FinancialDetailsVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *titles;
@property(nonatomic,strong)UserInfoModel *model;
@property(nonatomic,strong)UILabel *money;
@property(nonatomic,strong)UILabel *number;
@end

@implementation FinancialDetailsVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navBarBgAlpha=@"0.0";
//    // 改变NavBar顶层View的颜色
    self.navigationController.navigationBar.barTintColor =  [UIColor whiteColor];
    self.back.backgroundColor=[UIColor colorWithHexString:@"#64BAF6"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self refreshAccount];
    self.title=Localized(@"资金明细");
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
     [self addHeadView];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).priorityHigh();
        make.bottom.equalTo(self.view).with.offset(0).priorityHigh();
    }];
    
    
    // Do any additional setup after loading the view.
}
-(void)addHeadView{
    UIView *head=[[UIView alloc]init];
    head.backgroundColor=[UIColor colorWithHexString:@"#64BAF6"];
    
    UILabel *balance=[[UILabel alloc]init];
    [head addSubview:balance];
    balance.text=Localized(@"账户余额");
    balance.font=[UIFont systemFontOfSize:15];
    balance.textColor=[UIColor whiteColor];
    [balance mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(15);
    }];
    
    self.money=[[UILabel alloc]init];
    [head addSubview:self.money];
    self.money.text=[NSString stringWithFormat:@"₱%@",self.moneyStr];
    self.money.font=[UIFont systemFontOfSize:40];
    self.money.textColor=[UIColor whiteColor];
    [self.money mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.equalTo(balance.mas_bottom).offset(10);
    }];
    
    UILabel *integral=[[UILabel alloc]init];
    [head addSubview:integral];
    integral.text=Localized(@"账户积分");
    integral.font=[UIFont systemFontOfSize:15];
    integral.textColor=[UIColor whiteColor];
    [integral mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.equalTo(self.money.mas_bottom).offset(10);
    }];
    
    self.number=[[UILabel alloc]init];
    [head addSubview:self.number];
    self.number.text=self.pointStr;
    self.number.font=[UIFont systemFontOfSize:40];
    self.number.textColor=[UIColor whiteColor];
    [self.number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.equalTo(integral.mas_bottom).offset(10);
        make.bottom.equalTo(head.mas_bottom).offset(-15);
    }];
    
    //添加webview前计算一次高度
    CGFloat height = [head systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = head.bounds;
    frame.size.height = height;
    head.frame = frame;
    [self.tableview setTableHeaderView:head];
    [head layoutIfNeeded];
}
-(void)rightBarAction{
    
}

- (void)refreshAccount{
    
    NSString *urlstr =  [NSString stringWithFormat:@"%@%@",HomeURL,@"account"];
    [httpTool ZBPostNetDataWith:urlstr withDic:nil andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([[NSString stringWithFormat:@"%@",responseDict[@"ret"]] isEqualToString:@"200"]) {
            NSMutableArray *data=responseDict[@"data"];
            self.model = [UserInfoModel yy_modelWithDictionary:[data firstObject]];
            self.money.text=[NSString stringWithFormat:@"₱%@",self.model.member_money];
            self.number.text=[NSString stringWithFormat:@"%@",self.model.member_point];
        }
    } andFailure:^{
        
    }];
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.titles.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *title=self.titles[section];
    return title.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FinancialDetailsCell *cell=[FinancialDetailsCell theShareCellWithTableView:tableView];
    cell.opaque=YES;
    cell.layer.drawsAsynchronously=YES;
    cell.layer.rasterizationScale=[UIScreen mainScreen].scale;
    NSMutableArray *title=self.titles[indexPath.section];
    cell.title.text=title[indexPath.row];
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    cell.img.image=[UIImage imageNamed:@"充值"];
                    break;
                case 1:
                    cell.img.image=[UIImage imageNamed:@"提现"];
                    break;
                case 2:
                    cell.img.image=[UIImage imageNamed:@"余额明细"];
                    break;
                default:
                    break;
            }
            break;
        case 1:
            switch (indexPath.row) {
                case 0:
                    cell.img.image=[UIImage imageNamed:@"积分充值"];
                    break;
                case 1:
                    cell.img.image=[UIImage imageNamed:@"积分明细"];
                    break;
                default:
                    break;
            }
            break;
        case 2:
            switch (indexPath.row) {
                case 0:
                    cell.img.image=[UIImage imageNamed:@"积分商城-1"];
                    break;

                default:
                    break;
            }
            break;
        default:
            break;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    [self moneyRecharge];
                    break;
                case 1:
                    [self moneyRecharge];
                    break;
                case 2:
                    [self detail];
                    break;
                default:
                    break;
            }
            break;
        case 1:
            switch (indexPath.row) {
                case 0:
                    [self buyPoints];
                    break;
                case 1:
                    [self integralDetail];
                    break;
                default:
                    break;
            }
            break;
        case 2:
            switch (indexPath.row) {
                case 0:
                    
                    break;
            }
            break;
        default:
            break;
    }
}
-(void)moneyRecharge{
    self.hidesBottomBarWhenPushed=YES;
    MoneyRechargeVC *chatController = [[MoneyRechargeVC alloc] init];
    [self.navigationController pushViewController:chatController animated:YES];
//    self.hidesBottomBarWhenPushed=NO;
}
-(void)detail{
    self.hidesBottomBarWhenPushed=YES;
    DetailViewController *chatController = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:chatController animated:YES];
    //    self.hidesBottomBarWhenPushed=NO;
}
-(void)integralDetail{
    self.hidesBottomBarWhenPushed=YES;
    IntegralViewController *chatController = [[IntegralViewController alloc] init];
    [self.navigationController pushViewController:chatController animated:YES];
    //    self.hidesBottomBarWhenPushed=NO;
}
-(void)buyPoints{
    self.hidesBottomBarWhenPushed=YES;
    BuyPointsViewController *chatController = [[BuyPointsViewController alloc] init];
    [self.navigationController pushViewController:chatController animated:YES];
    //    self.hidesBottomBarWhenPushed=NO;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //分割线补全
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//MARK:-GETTER
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.showsVerticalScrollIndicator=NO;
        //        _myTableview.emptyDataSetSource=self;
        //        _myTableview.emptyDataSetDelegate=self;
        _tableview.scrollEnabled = YES;
    }
    return _tableview;
}
-(NSMutableArray *)titles{
    if (!_titles) {
        _titles=@[@[Localized(@"余额充值"), Localized(@"余额提现"),Localized(@"余额明细")],@[Localized(@"积分充值"),Localized(@"积分明细")],@[Localized(@"积分商城")]].copy;
    }
    return _titles;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
